package org.opengeoingest.tufts.Layer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 * create copies of Solr data but with a different layer id
 * this is used to generate additional layers so we can test with many tens of thousands of layers 
 * it loops over directories of data and makes copies of it
 * @author smcdon08
 *
 */
public class TestDataGenerator
{

	/**
	 * read the passed file into memory, change the layer id, write it out to the output file
	 * @param fileName
	 * @param outputFile
	 * @param layerIdSuffix
	 */
	private void processFile(File fileName, File outputFile, String layerIdSuffix)
	{
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try
		{
			inputStream = new FileInputStream(fileName);
			outputStream = new FileOutputStream(outputFile);
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			documentBuilderFactory.setValidating(false); // dtd isn't available
			documentBuilderFactory
					.setFeature(
							"http://apache.org/xml/features/nonvalidating/load-external-dtd",
							false);
			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();
			Document document = documentBuilder.parse(inputStream);
			
			// modify document
			// LayerId is expected to be the first field in the file
			// this is a hack, we should check the attribute name
			Node layerIdNode = document.getElementsByTagName("field").item(0);
			// get the LayerId and change it
			String layerId = layerIdNode.getTextContent();
			layerIdNode.setTextContent(layerId + layerIdSuffix);
			
			// now save the changed DOM/document out to disk
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(outputStream);
			transformer.transform(source, result);
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}
		catch (SAXException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (TransformerConfigurationException e)
		{
			e.printStackTrace();
		}
		catch (TransformerException e)
		{
			e.printStackTrace();
		}
		finally
		{
			closeInput(inputStream);
			closeOutput(outputStream);
		}

	}
	
	private static void closeInput(InputStream inputStream)
	{
		if (inputStream != null)
		{
			try
			{
				inputStream.close();
			}
			catch (IOException e)
			{
				;
			}
		}
	}

	private static void closeOutput(OutputStream outputStream)
	{
		if (outputStream != null)
		{
			try
			{
				outputStream.close();
			}
			catch (IOException e)
			{
				;
			}
		}
	}

	/**
	 * loop over all metadata files in the hard-coded directories and process each file
	 * @param args
	 */
	public static void main(String[] args)
	{
		int batch = 4;
		String idSuffix = "__" + batch;
		File outputDirectory = new File("/Users/smcdon08/tmp/OpenGeoPortal/ScaleTestData/" + batch);
		
		System.out.println("top of TestDataGenerator");
		TestDataGenerator testDataGenerator = new TestDataGenerator();

		// we loop over all the FGDC files in the following directories
		String[] directories = { "/Users/smcdon08/tmp/OpenGeoPortal/HarvardSolrData/tmp", "/Users/smcdon08/tmp/OpenGeoPortal/MitSolrData",
				"/Users/smcdon08/tmp/OpenGeoPortal/MassGisSolrData", 
				"/Users/smcdon08/tmp/OpenGeoPortal/BerkeleySolr/AllSolrRecords", "/Users/smcdon08/tmp/OpenGeoPortal/GistSolr"};

		for (String tempDirectory : directories)
		{
			File currentDirectory = new File(tempDirectory);
			File[] solrFiles = currentDirectory.listFiles();
			// loop over all the files in the directory and process the xml
			for (File currentSolrFile : solrFiles)
			{
				String filename = currentSolrFile.getName();
				if (filename.endsWith(".xml"))
				{
					File outputFile = new File(outputDirectory, filename);
					System.out.println("processing file " + currentSolrFile);
					testDataGenerator.processFile(currentSolrFile, outputFile, idSuffix);
				}

			}
		}
		// all done processing files, create report for user
		System.out.println("end of report");
		
	}
}
